programming4us
           
 
 
Programming

Unit Testing in Visual Studio 2010 (part 2) - Running a battery of tests

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
1/14/2012 3:44:49 PM

2. Running a battery of tests

You should be able to run your tests with the click of a mouse or by entering a single command. To run these tests, right-click the test project in Solution Explorer and choose Debug => Start New Instance. The unit test project executes and displays the results in the Test Results menu, as shown in Figure 5.

Based on the results in Figure 5, it looks like the unit tests aren't complete enough to adequately test the code. In this case, both unit tests failed because SayHello()CreateMessage() throw System.ArgumentException if the parameter "name" is empty. In this case, you have to tell the unit test framework and that you're expecting these methods to throw exceptions if the parameter "name" is null or empty. To do so, you add an ExpectedException attribute to each of the test methods:

[ExpectedException(typeof(System.ArgumentException))]
public void SayHelloTest()...

Adding an ExpectedException tells the unit test framework that you're expecting your methods to throw an exception of type System.ArgumentException or System.ArgumentNullException because it's derived from System.ArgumentException. Now, when you pass string.Empty or null to these methods, the unit tests won't fail because you're expecting these methods to throw exceptions when these values are provided for the parameter "name". So to adequately test these methods, you need to provide an empty string, a null string, and a valid string to each of these methods. You also want to test the negative case where the expected value isn't equal to the actual value. Listing 3 shows the complete unit test for CreateMessage.


Figure 5. The Test Results Window in Visual Studio.

Listing 3.
/// <summary>
///A test for CreateMessage
///</summary>
[TestMethod()]
[DeploymentItem("WindowsFormsApplication1.exe")]
[ExpectedException(typeof(System.ArgumentException))]
public void CreateMessageTest()
{
Form1_Accessor target = new Form1_Accessor();
string name = "Andrew";
string expected = "Hello Andrew!";
string actual;
actual = target.CreateMessage(name);
Assert.AreEqual(expected, actual);

name = string.Empty;
expected = string.Empty;

actual = target.CreateMessage(name);
Assert.AreEqual(expected, actual);

name = null;
expected = null;
actual = target.CreateMessage(name);

Assert.AreEqual(expected, actual);

name = "Andrew";
expected = "Hello Joe!";

Assert.AreNotEqual(expected, actual);
}


The first test sets the parameter "name" to "Andrew". In this test, you expect the resulting message to be "Hello Andrew!" as indicated by the Assert.AreEqual(). The second test passes an empty string as the "name" parameter. Because you provided the ExpectedException attribute for the System.ArgumentException, this tests two passes. The third test passes null as the "name" parameter. This time, too, the results are what you expect because CreateMessage() throws a System.ArgumentNullException. The final test tests the negative case where "Andrew" is again passed to CreateMessage(), and you test to ensure that the result isn't equal to something other than "Hello Andrew!". In this case, "Hello Joe!" doesn't match, and the Assert.AreNotEqual is true. Now, in Figure 6, Visual Studio shows that your tests have succeeded.


Figure 6. Display the results of your successful unit tests.
Other -----------------
- Microsoft ASP.NET 3.5 : AJAX-Enabled Web Services - Remote Calls via Page Methods
- Microsoft ASP.NET 3.5 : WCF Services for ASP.NET AJAX Applications
- Mobile Game Networking Essentials : Network Programming and J2ME
- Mobile Game Networking Essentials : Multiplayer Game Basics & Network Game Problems and Solutions
- Software Testing with Visual Studio Team System 2008 : Debug and running web test (part 2) - Running the test
- Software Testing with Visual Studio Team System 2008 : Debug and running web test (part 1) - Settings for .testrunconfig file
- Visual Studio Team System 2008 : Web test editor (part 3) - Toolbar properties
- Visual Studio Team System 2008 : Web test editor (part 2) - Other request properties
- Visual Studio Team System 2008 : Web test editor (part 1) - Web test properties & Web test request properties
- Build Mobile Websites and Apps for Smart Devices : Design for Mobile - Standing on the Shoulders of Giants
- Build Mobile Websites and Apps for Smart Devices : Design for Mobile - Build a Better Mouse
- Developing BlackBerry Tablet Applications with Flex 4.5 : Create a Flex Mobile Project (part 4) - Reading and setting author information for debug
- Developing BlackBerry Tablet Applications with Flex 4.5 : Create a Flex Mobile Project (part 3) - Setup Device
- Developing BlackBerry Tablet Applications with Flex 4.5 : Create a Flex Mobile Project (part 2) - Setup Simulator
- Developing BlackBerry Tablet Applications with Flex 4.5 : Create a Flex Mobile Project (part 1)
- Programming Excel with VBA and .NET : Procedures - Properties & Events
- Programming Excel with VBA and .NET : Procedures - Arguments and Results
- LINQ to Objects : Writing Basic Queries - How to Filter the Results (Where Clause)
- LINQ to Objects : Writing Basic Queries - Query Syntax Style Options
- DirectX 10 Game Programming : The 2D Resurgence - Sprites
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us